﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;


namespace Job.Core
{
    public class IISHelper
    {
        /// <summary>
        /// 添加主机头
        /// </summary>
        /// <param name="HostName">站点名称</param>
        /// <param name="strIP">IP地址</param>
        /// <param name="intPort">端口号</param>
        /// <param name="strDomain">主机头域名称</param>
        public static void AddHostHeader(string HostName, string strIP, int intPort, string strDomain)
        {
            try
            {
                DirectoryEntry iis = new DirectoryEntry("IIS://LOCALHOST/W3SVC");
                foreach (DirectoryEntry entry in iis.Children)
                {
                    if (string.Compare(entry.SchemaClassName, "IIsWebServer") == 0)
                    {
                        string strSiteName = entry.Properties["ServerComment"][0].ToString();//站点名称
                        if (strSiteName.ToLower() == HostName.ToLower())
                        {
                            PropertyValueCollection HeaderBindings = entry.Properties["ServerBindings"];
                            string strHeader = string.Format("{0}:{1}:{2}", strIP, intPort, strDomain.ToLower());
                            if (!HeaderBindings.Contains(strHeader))
                            {
                                HeaderBindings.Add(strHeader);
                                entry.CommitChanges();
                            }
                            break;
                        }
                    }
                }
            }
            catch
            { }
        }

        /// <summary>
        /// 删除主机头
        /// </summary>
        /// <param name="HostName">站点名称</param>
        /// <param name="strIP">IP地址</param>
        /// <param name="intPort">端口号</param>
        /// <param name="strDomain">主机头域名称</param>
        public static void DeleteHostHeader(string HostName, string strIP, int intPort, string strDomain)
        {
            try
            {
                DirectoryEntry iis = new DirectoryEntry("IIS://LOCALHOST/W3SVC");
                foreach (DirectoryEntry entry in iis.Children)
                {
                    if (string.Compare(entry.SchemaClassName, "IIsWebServer") == 0)
                    {
                        string strSiteName = entry.Properties["ServerComment"][0].ToString();//站点名称
                        if (strSiteName.ToLower() == HostName.ToLower())
                        {
                            PropertyValueCollection HeaderBindings = entry.Properties["ServerBindings"];
                            string strHeader = string.Format("{0}:{1}:{2}", strIP, intPort, strDomain.ToLower());
                            if (HeaderBindings.Contains(strHeader))
                            {
                                HeaderBindings.Remove(strHeader);
                                entry.CommitChanges();
                            }
                            break;
                        }
                    }
                }
            }
            catch
            { }
        }
    }
}
